﻿<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System;


using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Domain;
using DAO;

public class ImageHandler : IHttpHandler
{    
    public void ProcessRequest (HttpContext context)
    {
        context.Response.Clear();

        if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
        {
            long l = long.Parse(context.Request.QueryString["id"]);
            Imagen img = ImagenDAO.Traer(l);

            if (img != null)
            {
                // Now you have the id, do what you want with it, to get the right image
                // More than likely, just pass it to the method, that builds the image
                System.Drawing.Image image = Utils.byteArrayToImage(img.img);

                // Of course set this to whatever your format is of the image
                context.Response.ContentType = "image/jpeg";
                // Save the image to the OutputStream
                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);

                return;
            }
        }

        context.Response.ContentType = "text/html";
        context.Response.Write("<p>Need a valid id</p>");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
   }
}